<script setup>

// 路径导航
import {onMounted, reactive, ref} from "vue";
import {GENDER_OPTIONS, RULE} from "../../../const/index.js";
import {ElMessage} from "element-plus";
import router from "../../../router/index.js";
import MyNav from "../../../components/MyNav.vue";
import MyForm from "../../../components/MyForm.vue";
import {insertApi, listApi, updateApi} from "../../../api/axios.js";
import MyUpload from "../../../components/MyUpload.vue";
import {UPLOAD_ASSETS_URL} from "../../../api/assets.js";
import {getResponseData} from "../../../request/index.js";
import {UPLOAD_AVATAR_URL} from "../../../api/emp.js";
import {genderFormat} from "../../../util/index.js";

// 所在部门下拉菜单选项
let deptOptions = ref([]);


// 员工信息
let emp = JSON.parse(sessionStorage.getItem('row'));

// 路径导航
const navItems = [
  {icon: 'Avatar', label: '用户管理', url: '/Emp'},
  {icon: 'User', label: '员工列表', url: '/Emp'},
  {icon: 'Edit', label: '修改员工信息'},
];

// 表单项 + 表单值 + 表单规则
let items = ref([
  {label: '登录账号', prop: 'username', span: 12, disabled: true},
  {label: '真实姓名', prop: 'realname', span: 12, required: true},
  {label: '手机号码', prop: 'phone', span: 12, required: true},
  {label: '身份证号', prop: 'idcard', span: 12, required: true},
  {label: '微信号码', prop: 'wechat', span: 12, required: true},
  {label: '电子邮箱', prop: 'email', span: 12, required: true},
  {label: '员工性别', prop: 'gender', span: 12, required: true, type: 'select', options: GENDER_OPTIONS},
  {label: '员工年龄', prop: 'age', span: 12, required: true, type: 'number'},
  {label: '所在部门', prop: 'fkDeptId', span: 12, required: true, type: 'select', options: deptOptions.value},
  {label: '入职日期', prop: 'hiredate', type: 'datetime', span: 12, required: true},
  {label: '现居住地', prop: 'address', required: true, type: 'textarea', rows: 2},
  {label: '员工描述', prop: 'info', type: 'textarea', rows: 6},
]);
let params = reactive(emp);
let rules = {title: RULE.TITLE, info: RULE.INFO};

/* ======================================== 修改成功后 ================================================ */
function updateSuccess() {
  ElMessage.success('修改记录成功');
  setTimeout(() => router.push('/Emp'), 1000);
}

/* ======================================== 加载函数 ================================================ */

onMounted(async () => {
  // 查询全部部门，并添加到下拉菜单
  Object.values(getResponseData(await listApi({module: 'dept'}))).forEach(dept => {
    deptOptions.value.push({label: dept['title'], value: dept['id']});
  });
})
</script>

<template>
  <my-nav :items="navItems"/>
  <el-row :gutter="30" class="emp-update-body">
    <el-col :span="16">
      <el-card class="emp-update-card" header="修改员工信息">
        <my-form type="update"
                 :items="items"
                 :params="params"
                 :rules="rules"
                 :api="updateApi"
                 :args="{module: 'emp'}"
                 :callback="updateSuccess"/>
      </el-card>
    </el-col>
    <el-col :span="8">
      <el-card header="上传员工头像">
        <my-upload :url="UPLOAD_AVATAR_URL + '/' + emp['id']"
                   name="avatarFile"
                   :callback="updateSuccess"
                   :auto-upload="true"/>
      </el-card>
    </el-col>
  </el-row>


</template>

<style scoped lang="scss">
.emp-update-body {
  margin: 65px auto 0; // 外边距
}
</style>